package com.rest.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class TimeUtil {

    public static SimpleDateFormat simpleDateFormat8 = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat simpleDateFormat12 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat onlyMonth = new SimpleDateFormat("yyyy-MM");


    /**
     * 转换分钟数小时分钟的格式
     */
    public static String getHhmmFromMinute(int minute) {
        if (minute == 0) {
            return "00:00";
        }
        StringBuilder sb = new StringBuilder();
        int h = minute / 60;
        int m = minute % 60;

        sb.append(String.format("%02d", h));
        sb.append(":");
        sb.append(String.format("%02d", m));

        return sb.toString();
    }

    public static Date getDateTimeByString(String dateTimeStr) {
        try {
            return simpleDateFormat12.parse(dateTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getDateByString(String dateStr) {
        try {
            return simpleDateFormat8.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date formatDate(Date date) {
        String format = onlyMonth.format(date);
        try {
            return onlyMonth.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 转换秒数为小时分钟秒的格式
     */
    public static String getHhmmssFromMinute(int second) {
        if (second == 0) {
            return "00:00:00";
        }
        StringBuilder sb = new StringBuilder();
        int h = second / 3600;
        int m = (second - h * 3600) / 60;
        int s = second % 60;

        sb.append(String.format("%02d", h));
        sb.append(":");
        sb.append(String.format("%02d", m));
        sb.append(":");
        sb.append(String.format("%02d", s));
        return sb.toString();
    }

    public static String getDateString8(Date date) {
        if (null == date) {
            return "";
        }
        return simpleDateFormat8.format(date);
    }

    public static String getDateString12(Date date) {
        if (null == date) {
            return "";
        }
        return simpleDateFormat12.format(date);
    }

    /**
     * 获取当月第一条日期
     *
     * @return
     */
    public static Date getCurrentMonthBeginDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 获取当月最后一天日期
     *
     * @return
     */
    public static Date getCurrentMonthEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int maxDate = calendar.getActualMaximum(Calendar.DATE); //当月最大天数
        calendar.set(Calendar.DATE, maxDate);
        return calendar.getTime();
    }

    public static int getCurrentMonthMaxDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int maxDate = calendar.getActualMaximum(Calendar.DATE); //当月最大天数
        return maxDate;
    }

    /**
     * 获取当前6个月的开始结束日期
     *
     * @return
     */
    public static List<Map<String, String>> getBefore6Month() {
        List<Map<String, String>> returnList = new ArrayList<Map<String, String>>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        for (int i = 0; i < 6; i++) {
            Map<String, String> temp = new HashMap<>();
            calendar.set(Calendar.DATE, 1);
            String beginDate = simpleDateFormat8.format(calendar.getTime());
            temp.put("beginDate", beginDate);
            int maxDate = calendar.getActualMaximum(Calendar.DATE); //当月最大天数
            calendar.set(Calendar.DATE, maxDate);
            String endDate = simpleDateFormat8.format(calendar.getTime());
            temp.put("endDate", endDate);
            calendar.add(Calendar.MONTH, -1);

            returnList.add(temp);
        }
        return returnList;
    }

}
